Chuyển sang thư mục làm việc
setwd('C:/Users/ASUS/OneDrive/Máy tính/Crawler')
Khai báo các thư viện cần dùng
library(ggplot2)
library(plotly)
library(gapminder)
library(tidyverse)
library(e1071)
library(viridis)
library(hrbrthemes)
library(dplyr)
library(plyr)
library(readr)
library(ggthemes)
library(car)
library(ggpubr)
library(nortest)
Nhập dữ liệu
my_file <- list.files(pattern = "*.csv")
data <- ldply(my_file, read_csv)
nrow(data)
## [1] 7389
# Xem 10 dòng đầu tiên
head(data, 10)
## name trademark brand time_up price year_product style
## 1 Acura MDX 2009 Acura MDX 08/11/2021 5.00e+08 2009 SUV
## 2 Acura MDX 2007 Acura MDX 05/11/2021 4.45e+08 2007 SUV
## 3 Acura MDX 2006 Acura MDX 29/10/2021 4.15e+08 2006 SUV
## 4 Acura MDX 2009 Acura MDX 26/10/2021 5.50e+08 2009 SUV
## 5 Acura MDX 2006 Acura MDX 21/10/2021 3.90e+08 2006 SUV
## 6 Acura MDX 2003 Acura MDX 20/10/2021 3.00e+08 2003 SUV
## 7 Acura MDX 2007 Acura MDX 13/10/2021 5.15e+08 2007 SUV
## 8 Acura MDX 2008 Acura MDX 13/10/2021 4.50e+08 2008 SUV
## 9 Acura MDX 2009 Acura MDX 25/11/2021 6.30e+08 2009 SUV
## 10 Acura MDX 2007 Acura MDX 02/11/2021 6.50e+08 2007 SUV
## status origin KM local
## 1 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 120000 Gia Lai
## 2 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 180000 H<U+1EA3>i Duong
## 3 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 159000 H<U+1EA3>i Duong
## 4 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 160000 Hà N<U+1ED9>i
## 5 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 120000 Hà N<U+1ED9>i
## 6 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 95000 Tp.HCM
## 7 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 80000 Hà N<U+1ED9>i
## 8 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 79000 Tây Ninh
## 9 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 10000 Hà N<U+1ED9>i
## 10 Ðã qua s<U+1EED> d<U+1EE5>ng Nh<U+1EAD>p kh<U+1EA9>u 96000 Tp.HCM
## box_car fuel
## 1 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 2 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 3 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 4 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 5 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 6 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 7 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 8 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 9 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
## 10 S<U+1ED1> t<U+1EF1> d<U+1ED9>ng Xang
# Tên các trường dữ liệu
names(data)
## [1] "name" "trademark" "brand" "time_up" "price"
## [6] "year_product" "style" "status" "origin" "KM"
## [11] "local" "box_car" "fuel"
# Kiểu dữ liệu mỗi trường
str(data)
## 'data.frame': 7389 obs. of 13 variables:
## $ name : chr "Acura MDX 2009" "Acura MDX 2007" "Acura MDX 2006" "Acura MDX 2009" ...
## $ trademark : chr "Acura" "Acura" "Acura" "Acura" ...
## $ brand : chr "MDX" "MDX" "MDX" "MDX" ...
## $ time_up : chr "08/11/2021" "05/11/2021" "29/10/2021" "26/10/2021" ...
## $ price : num 5.00e+08 4.45e+08 4.15e+08 5.50e+08 3.90e+08 3.00e+08 5.15e+08 4.50e+08 6.30e+08 6.50e+08 ...
## $ year_product: num 2009 2007 2006 2009 2006 ...
## $ style : chr "SUV" "SUV" "SUV" "SUV" ...
## $ status : chr "Ðã qua s<U+1EED> d<U+1EE5>ng" "Ðã qua s<U+1EED> d<U+1EE5>ng" "Ðã qua s<U+1EED> d<U+1EE5>ng" "Ðã qua s<U+1EED> d<U+1EE5>ng" ...
## $ origin : chr "Nh<U+1EAD>p kh<U+1EA9>u" "Nh<U+1EAD>p kh<U+1EA9>u" "Nh<U+1EAD>p kh<U+1EA9>u" "Nh<U+1EAD>p kh<U+1EA9>u" ...
## $ KM : num 120000 180000 159000 160000 120000 95000 80000 79000 10000 96000 ...
## $ local : chr "Gia Lai" "H<U+1EA3>i Duong" "H<U+1EA3>i Duong" "Hà N<U+1ED9>i" ...
## $ box_car : chr "S<U+1ED1> t<U+1EF1> d<U+1ED9>ng" "S<U+1ED1> t<U+1EF1> d<U+1ED9>ng" "S<U+1ED1> t<U+1EF1> d<U+1ED9>ng" "S<U+1ED1> t<U+1EF1> d<U+1ED9>ng" ...
## $ fuel : chr "Xang" "Xang" "Xang" "Xang" ...
View(data)
Xử lí dữ liệu
# Chuyển dữ liệu sang định dạng ngày
data$time_up <- as.Date(data$time_up, "%d/%m/%Y")
# Chuyển dữ liệu sang factor
# cols <- c("trademark", "brand", "style", "fuel")
# data[cols] <- lapply(data[cols], factor)
a. Tính các chỉ số thống kê mô tả
# mean
mean(data$price) # Trung bình giá xe
## [1] 1143322642
mean(data$KM) # Trung bình số KM đã đi
## [1] 59317.92
# mode
# Tạo hàm
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
getmode(data$price) # Yếu vị giá xe
## [1] 8.26e+08
getmode(data$KM) # Yếu vị số KM đã đi
## [1] 0
# median
median(data$price) # Trung vị giá xe
## [1] 5.19e+08
median(data$KM) # Trung vị số KM đã đi
## [1] 39000
# Bảng tóm tắt dữ liệu summary
summary(data)
## name trademark brand time_up
## Length:7389 Length:7389 Length:7389 Min. :2021-08-23
## Class :character Class :character Class :character 1st Qu.:2021-10-27
## Mode :character Mode :character Mode :character Median :2021-11-16
## Mean :2021-11-11
## 3rd Qu.:2021-11-29
## Max. :2021-12-05
## NA's :972
## price year_product style status
## Min. :1.200e+07 Min. :1975 Length:7389 Length:7389
## 1st Qu.:3.200e+08 1st Qu.:2013 Class :character Class :character
## Median :5.190e+08 Median :2018 Mode :character Mode :character
## Mean :1.143e+09 Mean :2016
## 3rd Qu.:8.260e+08 3rd Qu.:2020
## Max. :4.050e+10 Max. :2022
##
## origin KM local box_car
## Length:7389 Min. : 0 Length:7389 Length:7389
## Class :character 1st Qu.: 6000 Class :character Class :character
## Mode :character Median : 39000 Mode :character Mode :character
## Mean : 59318
## 3rd Qu.: 80000
## Max. :999999
##
## fuel
## Length:7389
## Class :character
## Mode :character
##
##
##
##
# table
# Số lượng xe của mỗi hãng đang được rao bán
table(data$trademark)
##
## Acura Alfa Romeo Audi BAIC
## 16 1 146 3
## Bentley BMW Cadillac Chery
## 25 144 3 2
## Chevrolet Chrysler Daewoo Daihatsu
## 297 2 287 31
## Ðô thành Dodge Dongben FAW
## 3 3 8 1
## Fiat Ford Fuso Geely
## 38 508 3 1
## Haima Hino Honda Hyundai
## 3 1 272 112
## Infiniti Isuzu JAC Jaguar
## 10 11 7 32
## Jeep Kia Lada Lamborghini
## 23 140 1 2
## Land Rover Lexus Lifan Lincoln
## 303 93 4 34
## Luxgen Maserati Mazda Mekong
## 8 9 482 3
## Mercedes-AMG Mercedes-Benz Mercedes-Maybach MG
## 2 434 9 105
## Mini Mitsubishi Nissan Peugeot
## 18 1273 230 77
## Porsche Ram Renault Rolls-Royce
## 55 2 12 15
## Samsung Ssangyong Subaru Suzuki
## 4 31 43 272
## SYM Tata Thaco Toyota
## 3 1 43 1325
## VinFast Volkswagen Volvo Xe t<U+1EA3>i
## 330 28 1 4
# Phân khúc xe đang được rao bán
table(data$style)
##
## City Car Convertible Coupe CUV Hatchback
## 1 14 45 79 977
## MPV Pick-up Truck Sedan Special Purpose Sport Car
## 1082 411 1958 4 9
## SUV Truck Van/Minivan Wagon
## 2414 135 242 18
# Các loại hộp số xe
table(data$box_car)
##
## Diesel S<U+1ED1> h<U+1ED7>n h<U+1EE3>p S<U+1ED1> t<U+1EF1> d<U+1ED9>ng S<U+1ED1> tay Xang
## 2 16 5266 2099 3
# Các tỉnh thành đang bán xe
table(data$local)
##
## An Giang B<U+1EA1>c Liêu B<U+1EAF>c Giang B<U+1EAF>c Ninh B<U+1EBF>n Tre
## 35 8 55 83 30
## Bà R<U+1ECB>a Vung Tàu Bình Ð<U+1ECB>nh Bình Duong Bình Phu<U+1EDB>c Bình Thu<U+1EAD>n
## 67 21 368 56 30
## C<U+1EA7>n Tho Cà Mau Cao B<U+1EB1>ng Ð<U+1EAF>k L<U+1EAF>k Ð<U+1EAF>k Nông
## 178 3 6 122 9
## Ð<U+1ED3>ng Nai Ð<U+1ED3>ng Tháp Ðà N<U+1EB5>ng Ði<U+1EC7>n Biên Gia Lai
## 296 28 250 2 47
## H<U+1EA3>i Duong H<U+1EA3>i Phòng H<U+1EAD>u Giang Hà Giang Hà N<U+1ED9>i
## 75 200 18 4 2296
## Hà Nam Hà Tinh Hòa Bình Hung Yên Khánh Hòa
## 45 22 15 34 61
## Kiên Giang Kon Tum L<U+1EA1>ng Son Lai Châu Lâm Ð<U+1ED3>ng
## 37 12 18 2 77
## Lào Cai Long An Nam Ð<U+1ECB>nh Ngh<U+1EC7> An Ninh Bình
## 11 40 105 63 29
## Ninh Thu<U+1EAD>n Phú Th<U+1ECD> Phú Yên Qu<U+1EA3>ng Bình Qu<U+1EA3>ng Nam
## 9 44 18 9 44
## Qu<U+1EA3>ng Ngãi Qu<U+1EA3>ng Ninh Qu<U+1EA3>ng Tr<U+1ECB> Sóc Trang Son La
## 24 58 12 13 11
## Tây Ninh Th<U+1EEB>a Thiên Hu<U+1EBF> Thái Bình Thái Nguyên Thanh Hóa
## 72 19 52 40 73
## Ti<U+1EC1>n Giang Tp.HCM Trà Vinh Tuyên Quang Vinh Long
## 47 1871 4 10 24
## Vinh Phúc Yên Bái
## 70 7
# Năm sản xuất của xe
table(data$year_product)
##
## 1975 1980 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
## 1 2 2 2 4 1 3 2 8 5 4 14 10 8 8 10
## 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
## 11 15 35 60 73 122 127 120 72 165 194 229 212 147 122 172
## 2014 2015 2016 2017 2018 2019 2020 2021 2022
## 283 379 541 474 558 845 628 1720 1
# Tứ phân vị quantile
# Giá xe
quantile(data$price)
## 0% 25% 50% 75% 100%
## 1.20e+07 3.20e+08 5.19e+08 8.26e+08 4.05e+10
# Số KM đã đi
quantile(data$KM)
## 0% 25% 50% 75% 100%
## 0 6000 39000 80000 999999
# Tương quan Correlation
cor(data[c(5, 6, 10)], use = "everything", method = "pearson")
## price year_product KM
## price 1.0000000 0.2265451 -0.1598730
## year_product 0.2265451 1.0000000 -0.4984613
## KM -0.1598730 -0.4984613 1.0000000
b. Vẽ biểu đồ và nhận xét về số liệu
Plot1: Biểu diễn số lượng xe đang bán của mỗi hãng
tab1 <- table(data$trademark)
tab1 <- data.frame(tab1)
colnames(tab1) <- c('Trademark', 'Freq')
p1 <- tab1 %>%
mutate(Trade = fct_reorder(Trademark, Freq)) %>%
ggplot(aes(x = Trade,
y = Freq,
fill = Trademark)) +
geom_bar(stat="identity", fill="#f68060", alpha=.6, width=.4) +
labs(title = "Number of cars of each brand being sold",
x = "Trademark",
y = "Count") +
coord_flip() +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = 'white'),
panel.border = element_rect(colour = '#990000', fill = NA, size = 1.5))
ggplotly(p1, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x")
Nhận xét biểu đồ:
- Biểu đồ là dạng cột ngang
- Biểu đồ biểu diễn số lượng xe đang được rao bán của mỗi hãng, trong đó:
+ Trục x biểu diễn tên của các hãng xe
+ Trục y biểu diễn số lượng xe
+ Nhỏ nhất là 1 xe đang được bán với 4 hãng (Volvo, Lada, Geely, Alfa Romeo)
+ Lớn nhất là hãng Toyota với số lượng 1325 chiếc đang được bán
Plot2: Tương quan giữa năm sản xuất sản phẩm và giá xe
Toyota <- filter(data, data$trademark == "Toyota")
Innova <- filter(Toyota, Toyota$brand == "Innova")
p2 <- Innova %>%
ggplot(aes(x = year_product,
y = price)) +
geom_boxplot(aes(fill = factor(year_product))) +
labs(title="Car price Toyota Innova",
caption = "Source: mpg",
x = "Year",
y = "Price",
fill = "") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5),
axis.title = element_text(face = "bold",
size = 14),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill="lightblue",
size=0.5,
linetype="solid",
colour ="darkblue"),
panel.border = element_rect(colour = '#990000',
fill = NA,
size = 1.5))
ggplotly(p2, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x",
title = list(text = paste0("Car price Toyota Innova",
"<br>",
"<sup>",
"How does year product affect car price?")))
Nhận xét biểu đồ:
- Biểu đồ dạng box plot
- Biểu đồ biểu diễn số mối quan hệ giữa giá xe và năm sản xuất của sảng phẩm, trong đó:
+ Trục x biểu diễn năm sản xuất của sản phẩm
+ Trục y biểu diễn giá tiền
+ Nhận thấy rằng, trong cùng một model xe (Toyota Innova) giá xe tăng khi năm sản xuất tăng, cụ thể:
* Năm 2006, trung bình giá xe là 210 triệu (max: 280 triệu, min: 145 triệu)
* Năm 2021, trung bình giá xe là 750 triệu (max: 887 triệu, min: 700 triệu)
Plot3: Xe có tỉ lệ bán nhiều nhất?
Porsche <- filter(data, data$trademark == "Porsche")
p3 <- Porsche %>%
ggplot(aes(x = year_product,
fill = brand,
text = style)) +
geom_density(alpha = 0.8) +
labs(title="Which is the highest rate of car sales in each model?",
caption = "Source: mpg",
x = "Year",
y = "Density",
fill = "Brand Car") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5),
axis.title = element_text(face = "bold",
size = 11),
title = element_text(face = "bold",
size = 12),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill="lightblue",
size=0.5,
linetype="solid",
colour ="darkblue"),
panel.border = element_rect(colour = '#990000',
fill = NA,
size = 1.5))
ggplotly(p3, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x",
title = list(text = paste0("Which is the highest rate of car sales in each model?",
"<br>",
"<sup>",
"From 2007-2021")))
Nhận xét biểu đồ:
- Biểu đồ dạng mật độ
- Biểu đồ biểu diễn tỉ lệ mỗi dòng xe đang được bán của hãng xe Porsche, trong đó:
+ Trục x biểu diễn năm sản xuất ra sản phẩm
+ Trục y biểu diễn tỉ lệ
- Xét dòng xe Porsche Cayman:
+ Dòng xe này được rao bán từ năm 2015 cho đến nay
+ Từ 2015-2019, tỉ lệ rao bán của dòng xe này tăng liên tục
+ Vào năm 2019, dòng xe này có tỉ lệ rao bán lớn nhất (0.214)
+ Từ 2019-2021, tỉ lệ này giảm nhẹ và đến 2021 đạt chỉ số 0.168
Plot4: Luxury car
car_4b <- filter(data, data$price >= 4000000000)
p4 <- car_4b %>%
ggplot(aes(x = trademark,
y = price,
size = price,
fill = local,
text = name)) +
geom_point() +
labs(title="Luxury Car",
caption = "Source: mpg",
x = "Trademark",
y = "Price",
fill = "Province") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(angle = 90,
vjust = 0.5,
face = "bold"),
axis.text.y = element_text(face = "bold"),
axis.title = element_text(face = "bold",
size = 11),
title = element_text(face = "bold",
size = 12),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill="lightblue",
size=0.5,
linetype="solid",
colour ="darkblue"),
panel.border = element_rect(colour = '#990000',
fill = NA,
size = 1.5))
ggplotly(p4, dynamicTicks = TRUE) %>%
layout(hovermode = "x",
title = list(text = paste0("Luxury Car",
"<br>",
"<sup>",
"Which province sells luxury cars?")))
1. Phân tích mối quan hệ tương quan
Phát biểu giả thuyết:
\(H_{0}\): Hai biến có mối quan hệ tương quan
\(H_{1}\): Hai biến độc lập với nhau
df1 <- data
df1$style[which(df1$style == "City Car")] <- 1
df1$style[df1$style == "Convertible"] <- 2
df1$style[df1$style == "Coupe"] <- 3
df1$style[df1$style == "CUV"] <- 4
df1$style[df1$style == "Hatchback"] <- 5
df1$style[df1$style == "MPV"] <- 6
df1$style[df1$style == "Pick-up Truck"] <- 7
df1$style[df1$style == "Sedan"] <- 8
df1$style[df1$style == "Special Purpose"] <- 9
df1$style[df1$style == "Sport Car"] <- 10
df1$style[df1$style == "SUV"] <- 11
df1$style[df1$style == "Truck"] <- 12
df1$style[df1$style == "Van/Minivan"] <- 13
df1$style[df1$style == "Wagon"] <- 14
df1$style <- as.numeric(df1$style)
with(df1, cor.test(style, price, method="kendall"))
##
## Kendall's rank correlation tau
##
## data: style and price
## z = 29.338, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.2503946
# Kendall's rank correlation tau
#
# data: style and price
# z = 29.338, p-value < 2.2e-16
# alternative hypothesis: true tau is not equal to 0
# sample estimates:
# tau
# 0.2503946
# Nhận xét:
# Vì p_value < 0.5 => Bác bỏ giả thuyết H0, nghĩa là r != 0 một cách có ý nghĩa thống kê, hai biến có tương quan tuyến tính với nhau
# tau = 0.2503946 => Mối tương quan yếu
with(df1, cor.test(year_product, price, method="spearman"))
##
## Spearman's rank correlation rho
##
## data: year_product and price
## S = 2.786e+10, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.5856449
# Spearman's rank correlation rho
#
# data: year_product and price
# S = 2.786e+10, p-value < 2.2e-16
# alternative hypothesis: true rho is not equal to 0
# sample estimates:
# rho
# 0.5856449
# Nhận xét:
# Vì p_value < 0.5 => Bác bỏ giả thuyết H0, nghĩa là r != 0 một cách có ý nghĩa thống kê, hai biến có tương quan tuyến tính với nhau
# rho = 0.5856449 => Mối tương quan mạnh
with(df1, cor.test(KM, price, method="kendall"))
##
## Kendall's rank correlation tau
##
## data: KM and price
## z = -47.35, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## -0.3762011
# Kendall's rank correlation tau
#
# data: KM and price
# z = -47.35, p-value < 2.2e-16
# alternative hypothesis: true tau is not equal to 0
# sample estimates:
# tau
# -0.3762011
# Nhận xét:
# Vì p_value < 0.5 => Bác bỏ giả thuyết H0, nghĩa là r != 0 một cách có ý nghĩa thống kê, hai biến có tương quan tuyến tính với nhau
# rho = -0.3762011 => Mối tương quan trung bình mang tính nghịch
price_origin <- df1[,c(5, 9)]
price_origin$origin[which(price_origin$origin != "Nhập khẩu")] <- 11
price_origin$origin[which(price_origin$origin == "Nhập khẩu")] <- 22
price_origin$origin <- as.numeric(price_origin$origin)
with(price_origin, cor.test(origin, price, method="kendall"))
##
## Kendall's rank correlation tau
##
## data: origin and price
## T = NA, p-value = NA
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## NA
# Kendall's rank correlation tau
#
# data: origin and price
# z = -12.099, p-value < 2.2e-16
# alternative hypothesis: true tau is not equal to 0
# sample estimates:
# tau
# -0.1151155
# Nhận xét:
# Vì p_value < 0.5 => Bác bỏ giả thuyết H0, nghĩa là r != 0 một cách có ý nghĩa thống kê, hai biến có tương quan tuyến tính với nhau
# tau = -0.1151155 => Mối tương quan yếu mang tính nghịch
2. Kiểm định giả thuyết
Vì dữ liệu bán xe ô tô khó tuân theo phân phối chuẩn nên chúng ta chuyển sang thực hiện kiểm định phi tham số.
Wilcoxon Signed-Rank Test (Alternative to two-sample t-test for independent samples)
# Xem trung vị của các nhóm
tapply(df1$price, df1$style, median, na.rm=TRUE)
## 1 2 3 4 5 6 7
## 480000000 3124500000 1699000000 445000000 320000000 527000000 535000000
## 8 9 10 11 12 13 14
## 398500000 1925000000 8558000000 950000000 260000000 252500000 151500000
wilcox.test(df1$price, df1$style, alternative='two.sided', paired=TRUE)
##
## Wilcoxon signed rank test with continuity correction
##
## data: df1$price and df1$style
## V = 27302355, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
# Wilcoxon signed rank test with continuity correction
#
# data: df1$style and df1$price
# V = 0, p-value < 2.2e-16
# alternative hypothesis: true location shift is not equal to 0
# Nhận xét
# Vì p_value < 0.5 nên chúng ta bác bỏ giả thuyết H0, giữa các dòng xe có sự khác biệt về giá
price_origin <- Innova[,c(5, 9)]
price_origin$origin[which(price_origin$origin != "Nhập khẩu")] <- 11
price_origin$origin[which(price_origin$origin == "Nhập khẩu")] <- 22
price_origin$origin <- as.numeric(price_origin$origin)
# Xem trung vị của các nhóm
tapply(price_origin$price, price_origin$origin, median, na.rm=TRUE)
## 11
## 419500000
# Kiểm định bằng Kruskal wallis
#kruskal.test(price ~ origin, data = price_origin)
# Kruskal-Wallis rank sum test
#
# data: price by origin
# Kruskal-Wallis chi-squared = 19.149, df = 1, p-value = 1.209e-05
# Nhận xét:
# Vì p_value < 0.05 nên chúng ta bác bỏ giả thuyết H0. Giữa xe nhập khẩu và xe trong nước có sự khác biệt về giá
wilcox.test(price_origin$price, price_origin$origin, alternative='two.sided', paired=TRUE)
##
## Wilcoxon signed rank test with continuity correction
##
## data: price_origin$price and price_origin$origin
## V = 68635, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0
# Wilcoxon signed rank test with continuity correction
#
# data: price_origin$price and price_origin$origin
# V = 68635, p-value < 2.2e-16
# alternative hypothesis: true location shift is not equal to 0
# Nhận xét
# Vì p_value < 0.5 nên chúng ta bác bỏ giả thuyết H0. Trong dòng xe Toyot Innova, giá tiền giữa xe nhập khẩu và xe trong nước có sự khác biệt về giá
# Xem trung vị của các nhóm
HN <- filter(df1, df1$local == "Hà Nội")
HCM <- filter(df1, df1$local == "Tp.HCM")
HN_HCM <- rbind(HN, HCM)
tapply(HN_HCM$price, HN_HCM$local, median, na.rm=TRUE)
## Tp.HCM
## 5.39e+08
HN_HCM$local[which(HN_HCM$local == "Hà Nội")] <- 111
HN_HCM$local[which(HN_HCM$local == "Tp.HCM")] <- 222
HN_HCM$local <- as.numeric(HN_HCM$local)
wilcox.test(HN_HCM$price, HN_HCM$local, alternative='less', paired=TRUE)
##
## Wilcoxon signed rank test with continuity correction
##
## data: HN_HCM$price and HN_HCM$local
## V = 1751256, p-value = 1
## alternative hypothesis: true location shift is less than 0
# Wilcoxon signed rank test with continuity correction
#
# data: HN_HCM$price and HN_HCM$local
# V = 1751256, p-value = 1
# alternative hypothesis: true location shift is less than 0
# Nhận xét
# Vì p_value > 0.05 nên chúng ta chấp nhận giả thuyết H0. Giá xe bán ở Hà Nội cao hơn giá xe bán ở HCM
Linear Regression
Porsche$style[which(Porsche$style != "Convertible")] <- 1
Porsche$style[which(Porsche$style != "Coupe")] <- 2
Porsche$style[which(Porsche$style != "CUV")] <- 3
Porsche$style[which(Porsche$style != "Sedan")] <- 4
Porsche$style[which(Porsche$style != "Sport Car")] <- 5
Porsche$style[which(Porsche$style != "SUV")] <- 6
Porsche$style <- as.numeric(Porsche$style
)
Porsche$origin[which(Porsche$origin != "Nhập khẩu")] <- 11
Porsche$origin[which(Porsche$origin == "Nhập khẩu")] <- 22
Porsche$origin <- as.numeric(Porsche$origin)
RegModel.1 <- lm(price~KM, data=Porsche)
summary(RegModel.1)
##
## Call:
## lm(formula = price ~ KM, data = Porsche)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.764e+09 -2.096e+09 -3.357e+08 1.053e+09 6.857e+09
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5447058582 431533404 12.623 <2e-16 ***
## KM -11570 7786 -1.486 0.143
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.523e+09 on 53 degrees of freedom
## Multiple R-squared: 0.03999, Adjusted R-squared: 0.02188
## F-statistic: 2.208 on 1 and 53 DF, p-value: 0.1432
# Call:
# lm(formula = price ~ KM, data = Porsche)
#
# Residuals:
# Min 1Q Median 3Q Max
# -4.764e+09 -2.096e+09 -3.357e+08 1.053e+09 6.857e+09
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 5447058582 431533404 12.623 <2e-16 ***
# KM -11570 7786 -1.486 0.143
# ---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
# Residual standard error: 2.523e+09 on 53 degrees of freedom
# Multiple R-squared: 0.03999, Adjusted R-squared: 0.02188
# F-statistic: 2.208 on 1 and 53 DF, p-value: 0.1432
# Linear Regression: price = 5447058582 - 11570*KM
scatterplot(Porsche$price, Porsche$KM)
abline(v = -11570, h = 5447058582, col = 'gray60')
p <- ggplot(Porsche, aes(price, KM,
fill = brand, size = price)) +
geom_point() +
geom_smooth(method = 'loess', formula = 'y ~ x',
fill = '#CC6600',colour = '#990099') +
labs(title="Linear Regression",
# caption = "Source: mpg",
x = "Price",
y = "KM",
fill = "Brand",
size = "") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(angle = 45,
vjust = 0.5),
axis.title = element_text(face = "bold",
size = 14),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(fill="lightblue",
size=0.5,
linetype="solid",
colour ="darkblue"),
panel.border = element_rect(colour = '#990000',
fill = NA,
size = 1.5))
ggplotly(p, dynamicTicks = TRUE) %>%
rangeslider() %>%
layout(hovermode = "x",
title = list(text = paste0("Linear Regression",
"<br>",
"<sup>",
"Price-KM")))
## Warning: plotly.js does not (yet) support horizontal legend items
## You can track progress here:
## https://github.com/plotly/plotly.js/issues/53